package com.sentrix.auto.printercenter.infrastructure.notification;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.google.common.eventbus.AsyncEventBus;
import com.google.common.eventbus.Subscribe;
import com.sentrix.auto.printercenter.application.PrinterService;
import com.sentrix.auto.printercenter.application.PrinterTaskService;
import com.sentrix.auto.printercenter.domain.event.AgentStatusChangedEvent;
import com.sentrix.auto.printercenter.infrastructure.agentserver.dto.PinterJobMsg;
import com.sentrix.auto.printercenter.infrastructure.agentserver.dto.PrinterStatusMsg;
import com.sentrix.auto.printercenter.infrastructure.util.PrintUtil;
import com.sentrix.auto.printercenter.infrastructure.util.SpringContextHolder;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Objects;


@Slf4j
@Component
public class AgentStatusNotifier implements AgentStatusChangedEvent.AgentStatusListener {

    @Resource(name = "async-event-bus")
    private AsyncEventBus asyncEventBus; // 注入 Guava 异步事件总线

    @PostConstruct
    public void autoRegister() {
        log.info("AgentStatusNotifier autoRegister");
        AgentStatusChangedEvent.registerListener(this);
        asyncEventBus.register(this);
    }

    @Override
    public void onStatusChanged(AgentStatusChangedEvent event) {
        log.info("AgentStatusChangedEvent: {}", JSONUtil.toJsonStr(event));
    }

    @Subscribe
    public void onAgentStatusChanged(AgentStatusChangedEvent event) {
        log.info("guava event: AgentStatusChangedEvent: {}", JSONUtil.toJsonStr(event));
    }


    /**
     * 打印机状态事件处理
     *
     * @param event
     */
    @Subscribe
    public void onPinterStatusChanged(PrinterStatusMsg event) {
        log.info("guava event: 打印机状态事件: {}", JSONUtil.toJsonStr(event));
        if (StringUtils.isBlank(event.getAgentCode())) {
            log.error("event.getAgentCode() is null");
            return;
        }
        PrinterService printerService = SpringContextHolder.getBean(PrinterService.class);
        printerService.onlineStatusChange(event.getAgentCode(), event.getPrinterName(), !StrUtil.equalsIgnoreCase(event.getStatusText(), "OFFLINE"));
    }

    /**
     * 打印机
     *
     * @param event
     */
    @Subscribe
    public void onPrintJobStatusChanged(PinterJobMsg event) {
        log.info("guava event: 打印任务事件: {}", JSONUtil.toJsonStr(event));
        PinterJobMsg.JOB_STATUS jobStatus = PinterJobMsg.JOB_STATUS.getByCode(event.getEventType());
        if (Objects.isNull(jobStatus)) {
            log.error("jobStatus is null");
            return;
        }
        String taskId = PrintUtil.extractTaskId(event.getJobName());
        PrinterTaskService taskService = SpringContextHolder.getBean(PrinterTaskService.class);
        switch (jobStatus) {
            case SPOOLING:
                taskService.taskSending(event.getAgentCode(), event.getPrinterName(), event.getJobId(), taskId);
                break;
            case PRINTING:
                taskService.taskPrinting(event.getAgentCode(), event.getPrinterName(), event.getJobId(), taskId);
                break;
            case RETAINED:
                log.info("任务保留");
                break;
            case COMPLETE:
                taskService.complete(event.getAgentCode(), event.getPrinterName(), event.getJobId(), taskId);
                break;
            case DELETING:
                log.info("任务删除中");
                break;
            case DELETED:
                log.info("任务已删除");
            case FAILED:
                taskService.failed(event.getAgentCode(), event.getPrinterName(), event.getJobId(), taskId);
                break;
            default:
                break;
        }
    }
}
